import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcom from '../components/Welcom.vue'
import Users from '../components/users/Users.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Cate from '../components/goods/Cate.vue'
import Params from '../components/goods/Params.vue'
import Report from '../components/report/Report.vue'
import GoodList from '../components/goods/List.vue'
Vue.use(VueRouter)

const routes = [
  
  {
    path:'/',redirect:'/login'
  },
  {
    path:"/login",
    component:Login,
  },

  {
    path:'/home',
    component:Home,
    redirect:'/welcom',//到了home组件就重定向到welcom这个页面
    children:
           [
             {
               path:'/welcom',
               component:Welcom,
             },
             
             {
               path:'/users',
               component:Users
             },
             {
              path:'/rights',
              component:Rights
             },
             {
               path:'/roles',
               component:Roles
             },
             {
               path:'/categories',
               component:Cate
             },
             {
               path:'/params',
               component:Params
             },
            {
              path:'/reports',
              component:Report
            }, {
              path:'/goods',
              component:GoodList
            },

           ],
        
          
  }
]

const router = new VueRouter({
  routes
})
// 挂载路由导航守卫  router.beforeEach(to,from,next)=>w
router.beforeEach((to,from,next) => {
  // to 将要访问函数
  // from 从哪个路径跳转而来
  // next 是一个函数 表示放行
  //   next()放行  next('/login)强制跳转
   if(to.path=== '/login') return next();//如果访问登录也 直接放行
// 获取token  因为只有登录成功才会有 token  若果没登录 则为空值
    const tokenStr = window.sessionStorage.getItem('token')

    if(!tokenStr) return next('/login')
    
    next()//前面通过了就放行

})

export default router
